<?php
    echo $this -> Html -> css('jquery.sidr.light');
    echo $this -> Html -> script('jquery.sidr.min');
 ?>
 
<?
   $scritto = $registro['ClasseDisciplinaDocente']['scritto']=='1'?true:false;
   $orale = $registro['ClasseDisciplinaDocente']['orale']=='1'?true:false;
   $pratica = $registro['ClasseDisciplinaDocente']['pratica']=='1'?true:false;
   $altro = $registro['ClasseDisciplinaDocente']['altro']=='1'?true:false;

?>
 <link href="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.4.4/bootstrap-editable/css/bootstrap-editable.css">
 <script src="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.4.4/bootstrap-editable/js/bootstrap-editable.min.js"></script>
 <script src="<?php echo $this->webroot;?>js/bootstrap-datepicker.it.js"></script>
    <div id="voto-detailed-modal" class="modal hide">
      <div class="modal-header">
        <h3><i class="icon-bookmark"></i> Inserisci il dettaglio del voto</h3>
      </div>
      <div class="modal-body">
        <p class="">
            <label>Domande:</label>
            <textarea class="span12" rows="5" style="border:1px dashed #ccc;"  name="Valutazione[domande]"></textarea>
        </p>
        <p>
            <label>Note eventuali:</label>
            <textarea class="span12" style="border:1px dashed #ccc;" rows="5"  name="Valutazione[note]"></textarea>
        </p>
      </div>
      <div class="modal-footer">
        <a href="#" class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Ok</a>
        <a href="#" class="btn btn-default" data-dismiss="modal" aria-hidden="true">Annulla</a>
      </div>
    </div>
    <!-- modale per la visualizzazione dei piani di lavoro -->
    <div id="plan-modal" class="modal hide">
      <div class="modal-header">
        <h3><i class="icon-book"></i> Piano di lavoro <small class="pull-right"><?php echo $plan["Plan"]["titolo"]?>
            <?php echo $plan["Plan"]["lock"]==1?"<i class='icon-lock'></i>":"<i></i>" ?></small>
            
        </h3>
      </div>
      <div class="modal-body">
        <div class="panel-group" id="accordion">
          <?php foreach($plan["PlanDetail"] as $planDetail ){?>
               <div  class="panel panel-default">
                <div class="panel-heading">
                  <h4 class="panel-title">
                    <a class="addPlanDetail" planDetailid="<?php echo $planDetail["id"]?>" style="text-decoration: none;"><i class="icon-arrow-down"></i></a>
                    <a class="title" data-toggle="collapse" data-parent="#accordion" href="#piano_<?php echo $planDetail["id"]?>">
                      <?php echo $planDetail["titolo"]?> 
                    </a> 
                    <small class="pull-right"> 
                        <?php echo $planDetail["data_previsione"]?> 
                        <?php if ($planDetail["data_svolgimento"]){ ?>
                            - <font color="green"><i class="icon-check"></i> <?php echo $planDetail["data_svolgimento"]?></font>
                         <?}?>
                    </small>
                  </h4>
                </div>
                <div id="piano_<?php echo $planDetail["id"]?>" class="panel-collapse collapse">
                  <div class="panel-body argomento">
                    <?php echo $planDetail["argomento"]?>
                  </div>
                </div>
              </div>
          <?}?>
        </div>
      </div>
      <div class="modal-footer">
        <a href="#" class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Ok</a>
        <a href="#" class="btn btn-default" data-dismiss="modal" aria-hidden="true">Annulla</a>
      </div>
    </div>
   <div id="sidr">
        <div id="header_nav"><i class="icon-unlock-alt icon-4x icon-border" style="position: relative;top: 88px;"></i></div>
        <table class="table">
            <thead>
                <tr>
                    <th>Proposta di Voto</th>    
                    <th>Voto di Consiglio</th>    
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><input type="text" name="" value="2"  /></td>
                    <td><input type="text" name="" value="2"  /></td>
                </tr>
                <tr>
                    <td><input type="text" name="" value="2"  /></td>
                    <td><input type="text" name="" value="2"  /></td>
                </tr>
                <tr>
                    <td><input type="text" name="" value="2"  /></td>
                    <td><input type="text" name="" value="2"  /></td>
                </tr>
                <tr>
                    <td><input type="text" name="" value="2"  /></td>
                    <td><input type="text" name="" value="2"  /></td>
                </tr>
                <tr>
                    <td><input type="text" name="" value="2"  /></td>
                    <td><input type="text" name="" value="2"  /></td>
                </tr>
                <tr>
                    <td><input type="text" name="" value="2"  /></td>
                    <td><input type="text" name="" value="2"  /></td>
                </tr>

            </tbody>
            
        </table>    
    </div>
   
   
   <div class="row hidden-desktop">
        <div class="span12" style="text-align: center;">
           <p class="lead"><i class="icon-calendar"></i> <a href="#" class="giorno_registro"><?php echo $this -> Time -> format('d/m/Y', $evento['Evento']['data']); ?></a></p>
        </div>
   </div>
   <div class="row hidden-desktop">
        <div class="span12" style="text-align: center;">
            <a class="btn btn-inverse lead sidr-menu" href="#sidr"> <i class="icon-legal lead"></i> Voti Finali </a>        
        </div>
   </div>
   <br />
    <div class="row-fluid">
        
         <div class="span3 visible-desktop" style="padding-left: 20px;">
            Data del Registro:<br>
           <i class="icon-calendar lead"></i>  <a href="#" class="giorno_registro lead"><?php echo $this -> Time -> format('d/m/Y', $evento['Evento']['data']); ?></a>
        </div>
        <div class="span2 pull-right visible-desktop">
           
            <a  class="btn btn-inverse lead sidr-menu" href="#sidr">  
            <i class="icon-legal lead"></i> Voti Finali </a>
        </div>
       
      </div>
   
    <div class="row-fluid">
        
        <div class="span12" style="text-align: center;">
          <small class="muted"><i class="icon-time"></i> Ultimo accesso : <?php echo $registro['Registro']['dt_last_login']?></small>
          <p class="lead" style="text-align:center;">Registro del Professore :
            <b> <?php echo $this->Session->read('Auth.User.Anagrafica.cognome')." ".$this->Session->read('Auth.User.Anagrafica.nome') ?> </b>
                - Classe : <b> <?php echo $registro['Classe']['anno'] . $registro['Classe']['sezione']; ?> </b>
                <br> Materia : <b> <?php echo $registro['Disciplina']['nome']; ?></b>
          </p>
        </div>
      </div>
      
   
      
      <div class="row-fluid">
        <div class="span8">
          <table class="registro table table-hover table-condensed table-striped" style="margin-top: 23px;">
            <thead>
              <tr>
                <th>#</th>
                <th>Cognome</th>
                <th>Nome</th>
                <th style="text-align: center;" >ER</th>
                <th style="text-align: center;" >UA</th>
                <th style="width:10px;">Presente</th>
                <?php if($scritto){?><th style="width:10px;">Scritto</th><?}?>
                <?php if($orale){?><th style="width:10px;">Orale</th><?}?>
                <?php if($pratica){?><th style="width:10px;">Pratica</th><?}?>
                <?php if($altro){?><th style="width:10px;">Altro</th><?}?>
              </tr>
            </thead>
            <tbody>
              <?php  foreach ($studentiEventoList as $key=>$studente) { ?>
                  <tr style="text-align: center;" data-nome-studente="<?php echo $studente['cognome'].' '.$studente['nome'] ?>">
                    <input type="hidden" value="<?php echo $studente['id']; ?>" />  
                    <td><?php echo $key+1 ?></td>
                    <td><?php echo $studente['cognome']?></td>
                    <td><?php echo $studente['nome']?></td>
                     <td class="center">
                         <input type="checkbox" class="er checkbox" <?php echo $studente['er']?'checked':''  ?> value="1" id="<?php echo $studente['id']; ?>_er" name="entrata_ritardo" />
                    </td> 
                    <td class="center">
                         <input type="checkbox" class="ua checkbox" <?php echo $studente['ua']?'checked':''  ?> value="1" id="<?php echo $studente['id']; ?>_ua" name="uscita_anticipata" />
                    </td>
                    <td class="center">
                        <i class="<?php echo $studente['presenza']==1?'icon-ok-circle':'icon-remove text-error'  ?>"></i>
                    </td>
                    <?php if($scritto){ ?>
                        <td class="center"><input id="<?php echo $studente['id']; ?>_1" type="text" data-studente="<?php echo $studente['id']; ?>" data-tipo="1" data-idVoto="<?php echo isset($studente['valutazione_1'])==true?$studente['valutazione_1']['id']:''?>"  name="Valutazione[voto]" value="<?php echo isset( $studente['valutazione_1'])==true?$studente['valutazione_1']['voto']:''?>" class="voto <?php echo (isset($studente['valutazione_1'])==true && ( $studente['valutazione_1']['domande']!="" || $studente['valutazione_1']['nota']!=""  ))?'detailed':'' ?>" /></td>
                   <?php }?>
                   <?php if($orale){ ?>
                        <td class="center"><input id="<?php echo $studente['id']; ?>_2" type="text" data-studente="<?php echo $studente['id']; ?>" data-tipo="2" data-idVoto="<?php echo isset($studente['valutazione_2'])==true?$studente['valutazione_2']['id']:''?>"  name="Valutazione[voto]" value="<?php echo isset( $studente['valutazione_2'])==true?$studente['valutazione_2']['voto']:''?>" class="voto <?php echo (isset($studente['valutazione_2'])==true && ( $studente['valutazione_2']['domande']!="" || $studente['valutazione_2']['nota']!=""  ))?'detailed':'' ?>" /></td>
                   <?php }?>
                   <?php if($pratica){ ?>
                        <td class="center"><input id="<?php echo $studente['id']; ?>_3" type="text" data-studente="<?php echo $studente['id']; ?>" data-tipo="3" data-idVoto="<?php echo isset($studente['valutazione_3'])==true?$studente['valutazione_3']['id']:''?>"  name="Valutazione[voto]" value="<?php echo isset($studente['valutazione_3'])==true?$studente['valutazione_3']['voto']:''?>" class="voto  <?php echo (isset($studente['valutazione_3'])==true && ( $studente['valutazione_3']['domande']!="" || $studente['valutazione_3']['nota']!=""  ))?'detailed':'' ?>" /></td>
                   <?php }?>
                   <?php if($altro){ ?>
                    <td class="center"><input id="<?php echo $studente['id']; ?>_4" type="text" data-studente="<?php echo $studente['id']; ?>" data-tipo="4" data-idVoto="<?php echo isset($studente['valutazione_4'])==true?$studente['valutazione_4']['id']:''?>"  name="Valutazione[voto]" value="<?php echo isset($studente['valutazione_4'])==true?$studente['valutazione_4']['voto']:''?>" class="voto  <?php echo (isset($studente['valutazione_4'])==true && ( $studente['valutazione_4']['domande']!="" || $studente['valutazione_4']['nota']!=""  ))?'detailed':'' ?>" /></td>
                   <?}?> 
                    <td class="center"><label class="media_voto"></label></td>
                  </tr>
                  
              <?php } ?>
              
              
            </tbody>
          </table>
        </div>
        <div class="span4">
            <table class="registro table table-hover table-condensed table-striped">
              <caption><label class="label label-info">Media voti dal 12/04/2013 al 12/04/2013</label></caption>
            <thead>
              <tr>
                <th style="width:10px;">Scritto</th>
                <th style="width:10px;">Orale</th>
                <th style="width:10px;">Pratica</th>
                <th style="width:10px;">Altro</th>
              </tr>
            </thead>
            <tbody>
              <tr style="text-align: center;">
                <td class="center">2</td>
                <td class="center"></td>
                <td class="center">5</td>
                <td class="center">5.25</td>
              </tr>
              <tr style="text-align: center;">
                <td class="center">2</td>
                <td class="center"></td>
                <td class="center">5</td>
                <td class="center">5.25</td>
              </tr>
              <tr style="text-align: center;">
                <td class="center">2</td>
                <td class="center"></td>
                <td class="center">5</td>
                <td class="center">5.25</td>
              </tr>
              <tr style="text-align: center;">
                <td class="center">2</td>
                <td class="center"></td>
                <td class="center">5</td>
                <td class="center">5.25</td>
              </tr>
              <tr style="text-align: center;">
                <td class="center">2</td>
                <td class="center"></td>
                <td class="center">5</td>
                <td class="center">5.25</td>
              </tr>
              <tr style="text-align: center;">
                <td class="center">2</td>
                <td class="center"></td>
                <td class="center">5</td>
                <td class="center">5.25</td>
              </tr>
              
            </tbody>
          </table>
            
        </div>
        </div>
        <div class="row-fluid">
            <form method="post" action="<?php echo $this->webroot; ?>Evento/update/<?php echo $evento['Evento']['id']; ?>">
            <div class="span4">
               <table class="registro table table-condensed ">
                <thead>
                  <tr>
                    <th>Note Disciplinari</th>
                  </tr>
                </thead>
                <tbody>
                  <tr style="text-align: center;">
                    <td>
                       <textarea name="Evento[nota]" class="" style="height: 300px;"><?php echo $evento['Evento']['nota']?> </textarea>
                    </td>
                  </tr>
                </tbody>
              </table>                
            </div>
            <div class="span8">
                 <table id="eventi_table" class="registro table table-striped table-hover" >
                    <thead>
                      <tr>
                        <th>Argomenti svolti <small class="pull-right"><a href="#plan-modal" data-toggle="modal">Piano di lavoro</a></small></th>
                        <th></th>
                      </tr>
                    </thead>
                    <tbody>
                         <tr id="plan-row-template" style="text-align: center;display:none;" >
                            <input type="hidden" class="planDetail_id" name="Evento[argomento][]" value="0" />
                            <td></td>
                            <td><a data-confirm onclick=""><i class="icon-remove"></i></a></td>
                          </tr>
                      <?php foreach($plan['PlanDetail'] as $key=>$planDetail){ 
                          if(in_array($planDetail["id"],explode(",",$evento["Evento"]["argomento"]))){
                          ?>
                          <tr  style="text-align: center;" >
                            <input type="hidden" name="Evento[argomento][]" value="<?php echo $planDetail["id"] ?>" />
                            <td>
                               <b><?php echo $planDetail['titolo']?></b> : <?php echo $planDetail['argomento']?>
                            </td>
                            <td>
                                <a data-confirm onclick="$(this).parent().parent().remove();"><i class="icon-remove"></i></a>
                            </td>
                          </tr>
                      <?php } } ?>
                    </tbody>
              </table>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span2 offset10">
                <button class="btn btn-info" type="submit">Salva Evento</button>
            </div>
        </div>
            </form>
      </div>
      <script>
      
      function salvaVotoFast(votofast){
            var jqxhr = $.post("<?php echo $this->webroot; ?>EventoStudente/saveUpdate",votofast, function(data) {
              console.log(data);
              if(data.esito=="OK"){
                $("#"+votofast.studente_id+"_"+votofast.valutazione_tipo).data('idvoto',data.idValutazione); 
                setFlash("Il voto : <b>'"+votofast.valutazione_voto+"'</b> per lo studente : <b>"+votofast.studente_nome+ "</b> è inserito con successo!"); 
              }
            },'json')
            .fail(function() { alert("Non è stato possibile inserire il voto, contattare l'amministratore."); })
     }
        
     $(function(){
        $('.addPlanDetail').on("click",function(){
            detailid = $(this).attr("plandetailid");
            //prendo il padre
            panel = $(this).parent().parent().parent();
            title = panel.find(".title").html();
            argomento = panel.find(".argomento").html();
            var plan = $("#plan-row-template").clone();
            plan.removeAttr("id");
            plan.find(".planDetail_id").val(detailid);
            plan.find("td:first").html("<b>"+title+"</b> : "+argomento).end().appendTo("#eventi_table tbody").show("fast");
            $("#plan-modal").modal("hide");
        });
        
        
        
        $('.sidr-menu').sidr({side: 'right'});
                
        $('.giorno_registro').editable({
            format: 'yyyy-mm-dd', 
            type:'date',
            placement:'bottom',
            title:'Cambia pagina del registro alla data:',   
            viewformat: 'dd/mm/yyyy', 
            clear:false,  
            validate: function(value) {
                
                var curr_date = value.getDate();
                var curr_month = (value.getMonth()+1)<10?'0'+(value.getMonth()+1):(value.getMonth()+1);
                var curr_year = value.getFullYear();
                dateformatted = (curr_year + "-" + curr_month + "-" + curr_date);

                window.location = '<?php echo $this -> webroot; ?>Registro/view/<?php echo $registro['Registro']['classe_disciplina_docente_id']?>/'+dateformatted+'/<?php echo $registro['Registro']['classe_id']?>/<?php echo $registro['Registro']['disciplina_id']?>';
		},
		datepicker: {
		language:'it',
		weekStart: 1,
		startDate:'01/09/2012',//da customizzare con festivita ?
		//endDate:'15/06/2013',
		autoclose:true
		}
		});


		$(".voto").bind('blur',function(){
		voto = $(this).val();
		voto_int = Math.floor(voto);
		voto_parz = (voto-voto_int).toFixed(2);
		//console.log(voto_parz);
		if(voto_parz>=0.10 && voto_parz<0.25)$(this).val(voto_int+0.25);
		if(voto_parz>0.25 && voto_parz<0.50)$(this).val(voto_int+0.50);
		if(voto_parz>0.50 && voto_parz<0.75)$(this).val(voto_int+0.75);
		if(voto_parz>0.75)$(this).val(voto_int+1);
		if(voto>10)$(this).val('10');

        if($(this).val()!=""){
            votofast={
                'valutazione_voto':$(this).val(),
                'valutazione_tipo':$(this).data('tipo'),
                'valutazione_id':$(this).data('idvoto'),
                'evento_id':<?php echo $evento['Evento']['id']?>,
                'studente_id':$(this).data('studente'),
                'studente_nome':$(this).parent().parent().data('nome-studente')
            };
            console.log(votofast);
            //console.log($(this).parent().parent());
            salvaVotoFast(votofast);
        }
    		//TODO:ora prendo tutti i voti della riga e calcolo la media
    		/*var tot = 0;
    		$(this).closest('tr').find('.voto').each(function(){
    		tot += Number($(this).val());
    		console.log('valore :'+$(this).val());
    		console.log(tot);
    		});
    		media = parseInt(tot)/4;
    		$(this).closest('tr').find('.media_voto').text(media);
    		*/
		});
		
		
		$(".checkbox").on("change",function(){
		   console.log("checked : "+$(this).attr("id"));
		   var valueToSend = "0";
		   if($(this).is(":checked")){
		     valueToSend = "1";  
		   }
		   var campo=$(this).attr("name");
		   var idEvento=$(this).attr("id").split("_")[0];
		   eventoStudente={
		       'id':idEvento,
		       'evento_id':<?php echo $evento['Evento']['id']?>,
		       'campo':campo,
		       'valueToSend': valueToSend,

		    };
		    console.log(eventoStudente);
            var jqxhr = $.post("<?php echo $this->webroot; ?>eventostudente/saveUpdateEvento",eventoStudente, function(data) {
              console.log(data);
              if(data.esito=="OK"){
                setFlash(data.messaggio); 
              }
            },'json')
            .fail(function() { alert("Non è stato possibile aggiornare il campo, contattare l'amministratore."); })
		    
		});
		
		
		//il doppio click attiva la funzionalita di voto dettagliato
		$('.voto').dblclick(function() {
    		voto = $(this).val();
    		voto_int = Math.floor(voto);
    		voto_parz = (voto-voto_int).toFixed(2);
    		//console.log(voto_parz);
    		if(voto_parz>=0.10 && voto_parz<0.25)$(this).val(voto_int+0.25);
    		if(voto_parz>0.25 && voto_parz<0.50)$(this).val(voto_int+0.50);
    		if(voto_parz>0.50 && voto_parz<0.75)$(this).val(voto_int+0.75);
    		if(voto_parz>0.75)$(this).val(voto_int+1);
    		if(voto>10)$(this).val('10');
    
    		$("#voto-detailed-modal").modal('show');
		
		});

		});
</script>